package com.dk.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dk.mapper.MenuMapper;
import com.dk.mapper.RoleMenuMapper;
import com.dk.pojo.Menu;
import com.dk.service.MenuService;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@org.springframework.stereotype.Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Map<String, Object>> getInfo(Integer pid) {
        String key = pid.toString();
        List<Menu> list = null;
        if (redisTemplate.hasKey(key)) {
            List<Map<String, Object>> result = (List<Map<String, Object>>)redisTemplate.boundValueOps(key).get();
            return result;
        }else {
            list = menuMapper.selectByInfoById(pid);
            List<Map<String, Object>> result = getMenuTree(list, 0);
            redisTemplate.boundValueOps(key).set(result);
            return result;
        }

        /*String key = "menu" + pid;
        ValueOperations ops = redisTemplate.opsForValue();
        Boolean aBoolean = redisTemplate.hasKey(key);
        if (aBoolean) {
            List<Map<String, Object>> list = new Gson().fromJson(
                    key, new TypeToken<List<Map<String, Object>>>() {
                    }.getType());
            return list;
        } else {
            List<Menu> list = menuMapper.selectByInfoById(pid);
            List<Menu> list1 = menuMapper.selectByExample(null);
            List<Map<String, Object>> result = getMenuTree(list, 0);
            String s = new Gson().toJson(result);
            ops.set(key, s);
            return result;
        }*/
    }



    @Override
    @Transactional
    public List<Map<String, Object>> getInfoZtree() {
       List<Menu> list = menuMapper.selectByExample(null);
       List<Map<String ,Object>> result = getMenuzTree(list,0);
        return result;
    }

    @Override
    @Transactional
    public int insert(Menu menu) {
        return menuMapper.insert(menu);
    }

    @Override
    public List<Map<String, Object>> getInfoZtreeChecked(Integer roleId) {
        List<Menu> menuList = menuMapper.selectByExample(null);
        List<Integer> mids =  roleMenuMapper.selectByRoleId(roleId);
        List<Map<String, Object>> infoZtreeChecked = getInfoZtreeChecked(menuList, 0, mids);
        return infoZtreeChecked;
    }

    @Override
    public int deleteById(Integer id) {
        return menuMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateInfo(Menu menu) {
        return menuMapper.updateByPrimaryKey(menu);
    }

    @Override
    public Menu selectById(Integer id) {
        return menuMapper.selectByPrimaryKey(id);
    }

    public  List<Map<String,Object>>  getMenuzTree(List<Menu> list,Integer id){
        List<Map<String,Object>> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = null;
            //传递的主键id和当前pid是否相等
            Menu menu = list.get(i);
            if (id.equals(menu.getParentid())){
                map =  new HashMap<>();
                map.put("name",menu.getText());
                map.put("url",menu.getUrl());
                map.put("id",menu.getId());
                map.put("children",getMenuzTree(list,menu.getId()));
            }
            if(map != null){
                List<Map<String,Object>> list1 = (List<Map<String, Object>>) map.get("children");
                if (list1.size() <= 0){
                    map.remove("children");
                }
                result.add(map);
            }
        }
        return result;
    }
    public  List<Map<String,Object>>  getMenuTree(List<Menu> list,Integer id){
        List<Map<String,Object>> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = null;
            //传递的主键id和当前pid是否相等
            Menu menu = list.get(i);
            if (id.equals(menu.getParentid())) {
                map =  new HashMap<>();
                map.put("text",menu.getText());
                map.put("url",menu.getUrl());
                map.put("id",menu.getId());
                map.put("nodes",getMenuTree(list,menu.getId()));
            }
            if(map != null){
                List<Map<String,Object>> list1 = (List<Map<String, Object>>) map.get("nodes");
                if (list1.size() <= 0){
                    map.remove("nodes");
                }
                result.add(map);
            }
        }
        return result;
    }
    public  List<Map<String,Object>>  getInfoZtreeChecked(List<Menu> list,Integer id,List<Integer> mids){
        List<Map<String,Object>> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = null;
            //传递的主键id和当前pid是否相等
            Menu menu = list.get(i);
            if (id.equals(menu.getParentid())) {
                map =  new HashMap<>();
                map.put("name",menu.getText());
                map.put("url",menu.getUrl());
                map.put("id",menu.getId());
                map.put("children",getInfoZtreeChecked(list,menu.getId(),mids));
                if (mids != null && mids.size() > 0 && mids.contains(menu.getId())){
                    map.put("checked",true);
                }
            }
            if(map != null){
                List<Map<String,Object>> list1 = (List<Map<String, Object>>) map.get("children");
                if (list1.size() <= 0){
                    map.remove("children");
                }
                result.add(map);
            }
        }
        return result;
    }

    @Override
    public List<String> findPermsByPersonId(Integer pid) {
        return menuMapper.findPermsByPersonId(pid) ;
    }
}
